package com.lineage.server.model;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1ChatParty {
  private static final Log _log = LogFactory.getLog(L1ChatParty.class);
  
  private final List<L1PcInstance> _membersList = new ArrayList<>();
  
  private L1PcInstance _leader = null;
  
  public void addMember(L1PcInstance pc) {
    try {
      if (pc == null)
        throw new NullPointerException(); 
      if (this._membersList.contains(pc))
        return; 
      if (this._membersList.isEmpty())
        setLeader(pc); 
      this._membersList.add(pc);
      pc.setChatParty(this);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void removeMember(L1PcInstance pc) {
    try {
      if (!this._membersList.contains(pc))
        return; 
      this._membersList.remove(pc);
      pc.setChatParty(null);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public boolean isVacancy() {
    return (this._membersList.size() < 8);
  }
  
  public int getVacancy() {
    return 8 - this._membersList.size();
  }
  
  public boolean isMember(L1PcInstance pc) {
    return this._membersList.contains(pc);
  }
  
  private void setLeader(L1PcInstance pc) {
    this._leader = pc;
  }
  
  public L1PcInstance getLeader() {
    return this._leader;
  }
  
  public boolean isLeader(L1PcInstance pc) {
    return (pc.getId() == this._leader.getId());
  }
  
  public String getMembersNameList() {
    String _result = new String("");
    Iterator<L1PcInstance> iterator = this._membersList.iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      _result = String.valueOf(String.valueOf(_result)) + pc.getName() + " ";
    } 
    return _result;
  }
  
  private void breakup() {
    try {
      L1PcInstance[] members = getMembers();
      L1PcInstance[] array;
      int length = (array = members).length;
      int i = 0;
      while (i < length) {
        L1PcInstance member = array[i];
        removeMember(member);
        member.sendPackets((ServerBasePacket)new S_ServerMessage(418));
        i++;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void leaveMember(L1PcInstance pc) {
    try {
      L1PcInstance[] members = getMembers();
      if (isLeader(pc)) {
        breakup();
      } else if (getNumOfMembers() == 2) {
        removeMember(pc);
        L1PcInstance leader = getLeader();
        removeMember(leader);
        sendLeftMessage(pc, pc);
        sendLeftMessage(leader, pc);
      } else {
        removeMember(pc);
        L1PcInstance[] array;
        int length = (array = members).length;
        int i = 0;
        while (i < length) {
          L1PcInstance member = array[i];
          sendLeftMessage(member, pc);
          i++;
        } 
        sendLeftMessage(pc, pc);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void kickMember(L1PcInstance pc) {
    try {
      if (getNumOfMembers() == 2) {
        removeMember(pc);
        L1PcInstance leader = getLeader();
        removeMember(leader);
      } else {
        removeMember(pc);
      } 
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(419));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public L1PcInstance[] getMembers() {
    return this._membersList.<L1PcInstance>toArray(new L1PcInstance[this._membersList.size()]);
  }
  
  public int getNumOfMembers() {
    return this._membersList.size();
  }
  
  private void sendLeftMessage(L1PcInstance sendTo, L1PcInstance left) {
    try {
      sendTo.sendPackets((ServerBasePacket)new S_ServerMessage(420, left.getName()));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
